/**
 * jason 
 *
 * request 数据请求封装
 * 
 * 基于 Promise 对象实现更简单的 request 使用方式，支持请求和响应拦截
 */
export default {
	config: {
		header: {
			'Content-Type': 'application/json;charset=UTF-8',
			'Content-Type': 'application/x-www-form-urlencoded'
		},
		data: {},
		method: "POST",
		dataType: "json",
		/* 如设为json，会对返回的数据做一次 JSON.parse */
		responseType: "text",
		success() {},
		fail() {},
		complete() {}
	},
	interceptor: {
		request: null,
		response: null
	},
	request(options) {
		if (!options) {
			options = {}
		}
		options.dataType = options.dataType || this.config.dataType
		options.url =options.url
		options.data = options.data || {}
		options.method = options.method || this.config.method
		options.header = options.header || this.config.header
		return new Promise((resolve, reject) => {
			let _config = null
			options.complete = (response) => {
				let statusCode = response.statusCode
				response.config = _config
				if (this.interceptor.response) {
					let newResponse = this.interceptor.response(response)
					if (newResponse) {
						response = newResponse
					}
				}
				// 统一的响应日志记录
				if (statusCode === 200) { //成功
					resolve(response);
				} else {
					reject(response)
				}
			}

			_config = Object.assign({}, this.config, options)
			_config.requestId = new Date().getTime()
			if (this.interceptor.request) {
				this.interceptor.request(_config)
			}
			
			uni.request(_config);
		});
	},
	uploadfiles(options){
		if (!options) {
			options = {}
		}
		options.url =options.url
		if(options.data['file[]']){
			options.files =options.data['file[]']
			options.filePath =options.data['file[]'][0].uri
			options.name ='file[]'
		}
		if(options.data['file']){
			options.filePath =options.data['file']
			options.name ='file'
		}
		if(options.data['noteFile']){
			options.filePath =options.data['noteFile']
			options.name ='noteFile'
		}
		if(options.data['appendix']){
			options.filePath =options.data['appendix']
			options.name ='appendix'
		}
		options.formData =options.data
		return new Promise((resolve, reject) => {
			options.complete = (response) => {
				let statusCode = response.statusCode
				if (this.interceptor.response) {
					let newResponse = this.interceptor.response(response)
					if (newResponse) {
						response = newResponse
					}
				}
				// 统一的响应日志记录
				if (statusCode === 200) { //成功
					resolve(response);
				} else {
					reject(response)
				}
			}
			console.log(options);
			
			uni.uploadFile(options);
		});
	},
	get(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'GET'
		return this.request(options)
	},
	post(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	},
	put(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'PUT'
		return this.request(options)
	},
	delete(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'DELETE'
		return this.request(options)
	}
}

